<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/


// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.view');

class JobBoardViewJAPayment extends JView
{
	function display($tmpl = null)
	{
		global $mainframe;
		
		switch ($this->getLayout()){
			case 'credit_confirm':
				$this->show_credit_confirm();
				break;
			case 'posting_plan_confirm':
				$this->show_posting_plan_confirm();
				break;	
				
			case 'payment_cancel':
				$this->show_payment_cancel();
				break;
			case 'payment_notify':
				$this->show_payment_notify();
				break;			
		}
			 	
		parent::display($tmpl);
	}
	
	function show_credit_confirm(){
		global $option, $mainframe, $jbconfig;
		
		$id = JRequest::getVar('cid', array(), '', 'array');
		JArrayHelper::toInteger($id);
		$id = $id?$id[0]:0;
		$Itemid = JRequest::getInt('Itemid', 0);
		$model = $this->getModel('japayment');

		if(!$id){
			JError::raiseWarning(1, JText::_("Please select a credit package."));
			$mainframe->Redirect(JRoute::_("index.php?option=$option&view=japosts&layout=buycredit&cid[]=$id"));
		}
		
		
		$item = $this->getModel('japayment')->getPackage('ja_credits', $id);
		$item_check = $this->getModel('japayment')->getPackage('ja_credits', $id);
		$this->assign('item', $item);
		
		$pay_id = JRequest::getInt('payment_method', 0);
		if($pay_id){
			$payment_method = get_payment_gateways($pay_id);
			$this->assign('payment_method', $payment_method[0]);
		}
		else{
			$pays_sys = get_payment_gateways();		
			$paysys = JHTML::_('select.genericlist', $pays_sys, 'payment_method', 'class="inputbox" size="1"', 'id', 'alias', $jbconfig['billing']->get('ja_default_pay_meth'));
			$this->assign('paysys', $paysys);
		}
		
		// Check TAX		
		$model =& $this->getModel('japayment');
		$tax = $model->checkTAX($item_check);
		$this->assignRef('tax', $tax);
		if($tax){
			$item_check->price = $tax->price;
		}
		
		// Check Coupon code
		$coupon = JRequest::getVar("coupon_code", "");
		if ( $coupon != "" ){			
		    $result = $model->checkCouponCode($coupon, $item_check);
		    if( $result !== false ){
		      JRequest::setVar("discount_amount", $result->discount_amount);
		      $this->assign('discountInfo', $result);
		    }
		}
		
		$this->assign('Itemid', JRequest::getVar('Itemid', ''));								
	}	
	
	function show_posting_plan_confirm(){
  		global $jbconfig, $mainframe;  
  		
  		$types = JobBoardConstant::getPostingPlan_Type();
  		$this->assignRef('types', $types);
  		$Itemid = JRequest::getInt('Itemid');
  		
  		$id = JRequest::getVar('cid', array(), '', 'array');
  		$id = isset($id[0])?(int)$id[0]:0;
  		if(!$id) {
  			JError::raiseWarning(1, JText::_("Please select a Posting Plan Package."));
			$mainframe->Redirect(JRoute::_("index.php?option=$option&view=japosts&layout=selectplan"));
  		}
  		
  		$product = $this->getModel('japayment')->getPackage('ja_posting_plans', $id);
  		$item_check = $this->getModel('japayment')->getPackage('ja_posting_plans', $id);  	
  		
  		if($jbconfig['currency']->get('mode')=='special_currency'){
			$product_id = $product->id;
			$alias = JFilterOutput::stringURLSafe($product->title);
			
			$user = JFactory::getUser();		
			$account = getAccountByUserID($user->id);
			
			$data = new stdClass();	    
		    $data->user_id = $user->id;
		    $data->credit_balance = (int)$account->credit_balance - (int)$product->price;
		      
		    if($data->credit_balance<0) {
		    	JError::raiseWarning(1, JText::_("Your {$jbconfig['currency']->get('currency_symbol', 'Credit')} is not enough to make this transaction. Please purchase more {$jbconfig['currency']->get('currency_symbol', 'Credit')}s to continue this transaction."));
				return $mainframe->Redirect(JRoute::_("index.php?option=com_jajobboard&view=japosts&layout=buycredit&cid[]=$product_id&alias=$alias&Itemid=$Itemid"));
		    }
  		}
  		
  			
        $this->assignRef('item', $product);
        
        $pays_sys = get_payment_gateways();		
		$paysys = JHTML::_('select.genericlist', $pays_sys, 'payment_method', 'class="inputbox" size="1"', 'id', 'alias', $jbconfig['billing']->get('ja_default_pay_meth'));
		$this->assign('paysys', $paysys); 
		
		$model =& $this->getModel('japayment');
		// Check TAX
		$tax = false;
		if( $jbconfig['currency']->get('mode') != 'special_currency' ){
			$tax = $model->checkTAX($item_check);			
			if($tax){
				$item_check->price = $tax->price;
			}
		}
		$this->assignRef('tax', $tax);
		
		// Check Coupon code
		$coupon = JRequest::getVar("coupon_code", "");
		if ( $coupon != "" ){			
		    $result = $model->checkCouponCode($coupon, $item_check);
		    if( $result !== false ){
		      JRequest::setVar("discount_amount", $result->discount_amount);
		      $this->assign('discountInfo', $result);
		    }
		}
  	}
	
	function show_payment_cancel(){		
		$pay_id = JRequest::getInt('payment_method', 0);
		$payment_method = get_payment_gateways($pay_id);
		require_once(JPATH_COMPONENT.DS."asset".DS."payments".DS.$payment_method[0]->name.".php");
		$payment = new $payment_method[0]->name($payment_method[0]);
		$payment->payment_cancel();
	}
	
	function show_payment_notify(){
		$model = $this->getModel('japayment');		
		
		$pay_name = JRequest::getVar('payment_method', '');
		$payment_method = get_payment_gateways(0, $pay_name);
		require_once(JPATH_COMPONENT.DS."asset".DS."payments".DS.$payment_method[0]->name.".php");
		$payment = new $payment_method[0]->name($payment_method[0]);
		$result = $payment->check_payment();
		
		
		global $setup,$mainframe,$Itemid,$permission,$usr;
		$model = $this->getModel('payment');		
		$ptype = JRequest::getVar('ptype','');
		require_once(JPATH_COMPONENT.DS."helper".DS."payments".DS.$ptype.".php");
		$deposit_setup = $model->getDepositType($ptype);
		$payment = new $ptype($deposit_setup);
		$result = $payment->check_payment();
		if (!$result) {
			$model->deposit_error();			
		}else {
			$model->complete_deposit($result);
		}
	}
}